package com.mz.mattress.client.service.impl;

import com.mz.mattress.api.entity.PlanDate;
import com.mz.mattress.api.redis.RedisKeys;
import com.mz.mattress.api.util.DateUtil;
import com.mz.mattress.client.mapper.DeviceMapper;
import com.mz.mattress.client.mapper.MemberMapper;
import com.mz.mattress.client.mapper.OrderMapper;
import com.mz.mattress.client.mapper.PlanDateMapper;
import com.mz.mattress.client.service.PlanDateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class PlanDateServiceImpl implements PlanDateService {

    @Autowired
    private PlanDateMapper planDateMapper;

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public void dateInit() {

//        String nowDate = DateUtil.getNowDate();
//        log.info("nowDate={}", nowDate);
//
//        LambdaQueryWrapper<PlanDate> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(PlanDate::getDates, nowDate);
//        PlanDate planDate = planDateMapper.selectOne(queryWrapper);
//        if (planDate == null) {
//
//            PlanConfig config = planConfigMapper.selectById(1);
//
//            int memberCount = RandomGenerator.nextInt(config.getMemberCountMin(), config.getMemberCountMax());
//            int order1Count = RandomGenerator.nextInt(config.getOrder1CountMin(), config.getOrder1CountMax());
//            int order3Count = RandomGenerator.nextInt(config.getOrder3CountMin(), config.getOrder3CountMax());
//            int order6Count = RandomGenerator.nextInt(config.getOrder6CountMin(), config.getOrder6CountMax());
//            int order10Count = RandomGenerator.nextInt(config.getOrder10CountMin(), config.getOrder10CountMax());
////            int orderTimesCount = RandomGenerator.nextInt(config.getOrderTimesCountMin(), config.getOrderTimesCountMax());
//
//            planDate = new PlanDate();
//            planDate.setDates(nowDate);
//            planDate.setMemberCount(memberCount);
//            planDate.setOrder1Count(order1Count);
//            planDate.setOrder3Count(order3Count);
//            planDate.setOrder6Count(order6Count);
//            planDate.setOrder10Count(order10Count);
////            planDate.setOrderTimesCount(orderTimesCount);
//            planDateMapper.insert(planDate);
//        }

    }

    @Override
    public void initCreateOrderData() {

        String nowDate = DateUtil.getNowDate();
        log.info("nowDate={}", nowDate);

        PlanDate planDate = planDateMapper.selectByDates(nowDate);
        if (planDate == null) {
            log.error("init_create_order_data_not_find");
            return;
        }
        if (planDate.getOrder1Count() == null
                || planDate.getOrder3Count() == null
                || planDate.getOrder6Count() == null
                || planDate.getOrder10Count() == null) {
            log.error("init_create_order_data_param_not_find");
            return;
        }
        if (planDate.getInitData() != null && planDate.getInitData() == 1) {
            log.info("init_create_order_data_has_init");
            return;
        }

        Integer total = planDate.getOrder1Count() + planDate.getOrder3Count() + planDate.getOrder6Count() + planDate.getOrder10Count();

        List<Integer> list = new ArrayList<>(total);

        //1次卡
        Integer order1Count = planDate.getOrder1Count();
        for (Integer i = 0; i < order1Count; i++) {
            list.add(2);
        }

        //3次卡
        Integer order3Count = planDate.getOrder3Count();
        for (Integer i = 0; i < order3Count; i++) {
            list.add(3);
        }

        //6次卡
        Integer order6Count = planDate.getOrder6Count();
        for (Integer i = 0; i < order6Count; i++) {
            list.add(4);
        }

        //10次卡
        Integer order10Count = planDate.getOrder10Count();
        for (Integer i = 0; i < order10Count; i++) {
            list.add(5);
        }

        Collections.shuffle(list);

        //清空
        Object object = null;
        do {
            object = redisTemplate.boundListOps(RedisKeys.CREATE_ORDER_QUEUE).rightPop();
        } while (object != null);

        //安排一天的任务  goodsID
        redisTemplate.boundListOps(RedisKeys.CREATE_ORDER_QUEUE).rightPushAll(list.toArray());

        log.info("init_create_order_data_success_size={}", list.size());

        PlanDate update = new PlanDate();
        update.setId(planDate.getId());
        update.setOrderTotal(total);
        update.setInitData(1);
        planDateMapper.updateById(update);

    }

    @Override
    public void initDeviceQueue() {
        Long size = redisTemplate.boundListOps(RedisKeys.PLAN_DEVICE_QUEUE).size();
        if (size != null && size < 30) {

            List<String> list = deviceMapper.selectDemoList();
            log.info("init_device_queue_push_device_to_queue_size=" + list.size());
            Collections.shuffle(list);
            redisTemplate.boundListOps(RedisKeys.PLAN_DEVICE_QUEUE).rightPushAll(list.toArray());

        }
    }

    @Override
    public void initOldMobile() {

        Long size = redisTemplate.boundListOps(RedisKeys.OLD_MOBILE_QUEUE).size();
        if (size != null && size > 100)
            return;

        List<String> list = memberMapper.mobileList();

        Collections.shuffle(list);
        log.info("init_old_mobile_push_old_mobile_to_queue_size=" + list.size());

        int step = 200;

        for (int i = 0; i < list.size(); i = i + step) {

            int max = i + step;
            if (max > list.size()) {
                max = list.size();
            }

            List<String> subList = list.subList(i, max);
            redisTemplate.boundListOps(RedisKeys.OLD_MOBILE_QUEUE).rightPushAll(subList.toArray());
        }

        log.info("init_old_mobile_push_old_mobile_to_queue_end");

    }

    @Override
    public void initBuyAgainMobile() {

        Long size = redisTemplate.boundListOps(RedisKeys.BUY_AGAIN_MOBILE_QUEUE).size();
        if (size != null && size > 100)
            return;

        List<String> list = memberMapper.mobileList();

        Collections.shuffle(list);
        log.info("init_old_mobile_push_old_mobile_to_queue_size=" + list.size());

        int step = 200;

        for (int i = 0; i < list.size(); i = i + step) {

            int max = i + step;
            if (max > list.size()) {
                max = list.size();
            }

            List<String> subList = list.subList(i, max);
            redisTemplate.boundListOps(RedisKeys.OLD_MOBILE_QUEUE).rightPushAll(subList.toArray());
        }

        log.info("init_old_mobile_push_old_mobile_to_queue_end");


    }

    @Override
    public void initCheckedOrder() {

        //确保每天执行一次
        Long size = redisTemplate.boundListOps(RedisKeys.CHECKED_ORDER_ID_QUEUE).size();
        if (size != null && size > 100)
            return;

        String endDate = DateUtil.getNowDayPlus(-2);

        //查找可核销订单 200
        List<Long> list = orderMapper.selectByTime(1, "2025-10-06", endDate);

        Collections.shuffle(list);

        log.info("init_checked_order_to_queue_size=" + list.size());

        int step = 100;

        for (int i = 0; i < list.size(); i = i + step) {

            int max = i + step;
            if (max > list.size()) {
                max = list.size();
            }

            List<Long> subList = list.subList(i, max);
            redisTemplate.boundListOps(RedisKeys.CHECKED_ORDER_ID_QUEUE).rightPushAll(subList.toArray());
        }

//        List<Long> subList = list.subList(0, 2);
//        redisTemplate.boundListOps(RedisKeys.CHECKED_ORDER_ID_QUEUE).rightPushAll(list.toArray());

        log.info("init_checked_order_to_queue_end");

//        Object object = redisTemplate.boundListOps(RedisKeys.CHECKED_ORDER_ID_QUEUE).leftPop();
//
//        Long orderId = (Long) object;
//
//        System.out.println(orderId);
//        System.out.println(object);


    }

    @Override
    public Map<String, Integer> monitorData() {

        Map<String, Integer> map = new HashMap<>();

        Long size = redisTemplate.boundListOps(RedisKeys.CREATE_ORDER_QUEUE).size();
        map.put(RedisKeys.CREATE_ORDER_QUEUE, size.intValue());

        Long size2 = redisTemplate.boundListOps(RedisKeys.OLD_MOBILE_QUEUE).size();
        map.put(RedisKeys.OLD_MOBILE_QUEUE, size2.intValue());

        Long size3 = redisTemplate.boundListOps(RedisKeys.PLAN_DEVICE_QUEUE).size();
        map.put(RedisKeys.PLAN_DEVICE_QUEUE, size3.intValue());

        Long size4 = redisTemplate.boundListOps(RedisKeys.PLAN_TASK_QUEUE).size();
        map.put(RedisKeys.PLAN_TASK_QUEUE, size4.intValue());

        Long size5 = redisTemplate.boundZSetOps(RedisKeys.PLAN_TASK_SCORE_QUEUE).size();
        map.put(RedisKeys.PLAN_TASK_SCORE_QUEUE, size5.intValue());

        Long size6 = redisTemplate.boundZSetOps(RedisKeys.CHECKED_ORDER_ID_QUEUE).size();
        map.put(RedisKeys.CHECKED_ORDER_ID_QUEUE, size6.intValue());

        return map;
    }

    public static void main(String[] args) {

        System.out.println(DateUtil.getNowDate());

        String endDate = DateUtil.getNowDayPlus(-2);

        System.out.println(endDate);

    }


}
